<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/30
 * Time: 11:00
 */
class UserController extends CommonController{
    private $userdata;   //用户数据库

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->userdata = include './Data/user.php';
    }

    /**
     *用户注册
     */
    public function register(){
//        判断
        if(IS_POST){
//            判断用户名是否存在
            foreach($this->userdata as $v){
                if($_POST['username']==$v['username']){
                    $this->error('用户名已存在');
                }
            }

//            判断邮箱格式是否正确
            $preg ="/^[0-9a-z]+\w?[0-9a-z]{0,5}@[0-9a-z]{2,10}\.(com|cn|net)$/is";
            if(!preg_match($preg,$_POST['email'])){
                $this->error('邮箱格式错误');
            }

//            判断两次密码输入是否正确
            if(md5($_POST['password'])!==md5($_POST['confirmpassword'])){
                $this->error('两次密码输入不一致');
            }

//            将确认密码移除，不需要存入数据库中
            unset($_POST['confirmpassword']);
            $_POST['password'] = md5($_POST['password']);
//            写入数据库中
            $this->userdata[] = $_POST;
            $this->putData('./Data/user.php',$this->userdata);

//            提示信息
            $this->success('注册成功','index.php?c=user&a=login');
        }
        include './View/user/register.php';
    }

    /**
     * 用户登录
     */
    public function login(){
//        判断
        if(IS_POST){
            foreach($this->userdata as $v){
                if($_POST['username']==$v['username']&&md5($_POST['password'])==$v['password']){
//                    判断验证码是否正确
                    if(strtolower($_POST['code'])!==strtolower($_SESSION['validate'])){
                        $this->error('验证码错误');
                    }

//                    7天自动登录
                    if(isset($_POST['auto'])){
                        setcookie(session_name(),session_id(),time()+24*3600*7,'/');
                    }else{
                        setcookie(session_name(),session_id(),0,'/');
                    }

//                    用session存储用户名
                    $_SESSION['username'] = $_POST['username'];
                    $this->success('登录成功','index.php?c=wishes&a=index');
                }else{
                    continue;
                }
            }
            $this->error('登录失败');
        }
        include './View/user/login.php';
    }

    /**
     * 用户注销
     */
    public function out(){
        //        销毁变量
        session_unset();
//        销毁文件
        session_destroy();
//        提示信息
        $this->success('注销成功','index.php?c=user&a=login');
    }

    /**
     * 异步验证注册用户名是否存在
     */
    public function validatename(){
        foreach($this->userdata as $v){
            if($v['username']==$_POST['username']){
                echo json_encode(['status'=>true]);die;
            }
        }
        echo json_encode(['status'=>false]);die;
    }

    /**
     * 异步验证注册邮箱格式是否正确
     */
    public function validateemail(){
        $preg = "/^[0-9a-z]+\w?[0-9a-z]{0,5}@[0-9a-z]{2,10}\.(com|cn|net)$/is";
        if(preg_match($preg,$_POST['email'])){
           echo json_encode(['status'=>true]);die;
        }else{
            echo json_encode(['status'=>false]);die;
        }
    }

    /**
     * 异步验证注册两次密码是否一致
     */
    public function validatetwo(){
        if(md5($_POST['confirmpassword'])==md5($_POST['password'])){
            echo json_encode(['status'=>true]);die;
        }else{
            echo json_encode(['status'=>false]);die;
        }

    }

    /**
     * 异步验证登录用户名是否存在
     */
    public function vusername(){
        foreach($this->userdata as $v){
            if($v['username']==$_POST['username']){
                echo json_encode(['status'=>true]);die;
            }
        }
        echo json_encode(['status'=>false]);die;
    }

    /**
     * 异步验证用户名密码是否正确
     */
    public function passuser(){
    foreach($this->userdata as $v){
        if($_POST['username']==$v['username']&&md5($_POST['password'])==$v['password']){
            echo json_encode(['status'=>true]);die;
        }
    }
    echo json_encode(['status'=>false]);die;
}

    /**
     * 异步验证登录验证码是否正确
     */
    public function vcode(){
        if(strtolower($_POST['code'])==strtolower($_SESSION['validate'])){
            echo json_encode(['status'=>true]);die;
        }else{
            echo json_encode(['status'=>false]);die;
        }
    }
}